/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.FeeBillBean;
import com.minierp.bean.FeeBillItemBean;
import com.minierp.dao.FeeBillDao;
import com.minierp.model.FeeBill;
import com.minierp.model.FeeBillItem;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class FeeBillValidator implements Validator {
	private FeeBillDao feeBillDao;

	public boolean supports(Class clazz) {
		return FeeBillBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		FeeBillBean bean = (FeeBillBean) obj;
		FeeBill form = bean.getFeeBill();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		boolean hasError = false;
		if (form.getTallySetId() == 0L) {
			hasError = true;
			errors.rejectValue("feeBill.tallySetId", "", "请选择『当前开单帐套』");
		}

		if (!hasError
				&& (bean.getCodeRule().getGenerateRule().equals("MANUAL") || bean
						.getOldId() != 0L)) {
			if (form.getBillCode() != null
					&& !form.getBillCode().trim().equals("")) {
				FeeBill hasItems = this.feeBillDao.get(form.getBillCode());
				if (hasItems != null && hasItems.getId() != form.getId()) {
					errors.rejectValue("feeBill.billCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("feeBill.billCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			try {
				form.setFeeDate(sf.parse(bean.getFeeDateStr()));
			} catch (Exception arg12) {
				hasError = true;
				errors.rejectValue("feeDateStr", "",
						"『日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		if (!hasError) {
			if (form.getDepartmentId() == 0L) {
				errors.rejectValue("feeBill.departmentId", "", "请选择『部门』");
				hasError = true;
			} else if (form.getBankAccountId() == 0L) {
				errors.rejectValue("feeBill.bankAccountId", "", "请选择『付款账户』");
				hasError = true;
			} else if (form.getManagerId() == 0L) {
				errors.rejectValue("feeBill.managerId", "", "请选择『经办人员』");
				hasError = true;
			}
		}

		if (!hasError && form.getMark() != null
				&& form.getMark().length() >= 2000) {
			hasError = true;
			errors.rejectValue("feeBill.mark", "", "『摘要』不能超过2000个字符");
		}

		boolean arg14 = false;
		BigDecimal costTotal = new BigDecimal(0);
		if (!hasError) {
			for (int i = 0; i < bean.getItems().size(); ++i) {
				FeeBillItemBean cb = (FeeBillItemBean) bean.getItems().get(i);
				FeeBillItem c = cb.getFeeBillItem();
				if (!cb.getStatus().equals("DELETE")) {
					try {
						c.setCost(new BigDecimal(cb.getCostStr()));
					} catch (Exception arg13) {
						hasError = true;
						errors.rejectValue("items", "", "费用明细的『金额』只能输入数字");
						break;
					}

					costTotal = costTotal.add(c.getCost());
					boolean hasInput = false;
					if (c.getCost().compareTo(new BigDecimal(0)) != 0
							|| c.getFtId() != 0L || c.getMark() != null
							&& !c.getMark().equals("") || c.getBbId() != 0L) {
						hasInput = true;
					}

					if (hasInput) {
						if (c.getFtId() == 0L) {
							hasError = true;
							errors.rejectValue("items", "", "请选择『费用类型』");
							break;
						}

						arg14 = true;
					} else {
						cb.setStatus("DELETE");
					}
				}
			}
		}

		if (!hasError && costTotal.compareTo(form.getCost()) != 0) {
			hasError = true;
			errors.rejectValue("items", "", "『付款金额』与费用明细总金额不符");
		}

		if (!hasError && !arg14) {
			hasError = true;
			errors.rejectValue("items", "", "费用明细不能为空");
		}

	}

	public FeeBillDao getFeeBillDao() {
		return this.feeBillDao;
	}

	public void setFeeBillDao(FeeBillDao feeBillDao) {
		this.feeBillDao = feeBillDao;
	}
}